package LearnDataStructure.a_线性结构.栈;

import LearnDataStructure.a_线性结构.栈.MyLinkedListDouble;
import LearnDataStructure.a_线性结构.栈.MyStackAPI;
import LearnDataStructure.a_线性结构.栈.StackNode;

import java.util.EmptyStackException;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-07-04 10:22
 * @description: LeetcodeLearnAlgorithm -> LearnDataStructure.a_线性结构.栈
 */
public class MyStackList<T> extends MyLinkedListDouble<T> implements MyStackAPI<T> {
    @Override
    public void push(T element) {
        super.add(element);
    }

    @Override
    public T pop() {
        if (super.size <= 0) {
            throw new EmptyStackException();
        }
        StackNode<T> top = super.tail.pre;
        T res = top.data;
        top.pre.next = super.tail;
        super.tail.pre = top.pre;
        top.pre = null;
        top.next = null;
        super.size--;
        return res;
    }

    @Override
    public boolean empty() {
        return getSize() == 0;
    }

    @Override
    public int getSize() {
        return super.size;
    }

    @Override
    public T peek() {
        return super.tail.pre.data;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}
